package com.qf.cloud2204.hystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.cloud2204.common.vo.ResultVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/h")
public class HystController {
    //开启降级功能，（附带熔断）
    @HystrixCommand(fallbackMethod = "fallback",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "10000"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "70"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000"),
    }
    ,threadPoolKey = "mypool",threadPoolProperties = {
            @HystrixProperty(name = "coreSize",value= "2"),
            @HystrixProperty(name = "maxQueueSize",value= "2")

    }
    )
    @GetMapping("/test1")
    public ResultVo test1(Integer num){
        System.out.println("当前处理请求的线程的名字："+Thread.currentThread().getName());
        //超时写法
//        try {
//            TimeUnit.SECONDS.sleep(3);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

  //      int a = 1/0;
        if (num==0){
            int a = 1/0;
        }else {
  //          System.out.println("恢复了！！！");
            //模拟时长
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"完成了请求");
        }
        return new ResultVo(1,"ok","123");
    }

    @GetMapping("/test2")
    public ResultVo test2(Integer num){
        System.out.println("当前处理请求的线程的名字："+Thread.currentThread().getName());
        return new ResultVo(1,"ok","456");
    }

    @GetMapping("/test3")
    public ResultVo test3(Integer num){
        //创建一个用于协调线程的对象 CountDownLatch
        CountDownLatch latch = new CountDownLatch(num);
        //根据传入的num的值给test1接口发松num数量的请求
        for (int i=0;i<num;i++) {
            //创建线程，由线程发送请求
            new Thread(new Runnable() {
                @Override
                public void run() {
                    RestTemplate restTemplate = new RestTemplate();
                    String url ="http://localhost:8110/h/test1?num=1";
                    System.out.println(Thread.currentThread().getName()+":"+"发送请求");
                    String result = restTemplate.getForObject(url, String.class);
                    System.out.println(Thread.currentThread().getName()+":"+result);
                    //让latch的计数器减1
                    latch.countDown();
                }
            }).start();
        }
        try {
            latch.await();//让当前线程进行等待，等到latch计数器归零
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ResultVo(1,"ok","456");
    }

    //降级方法,和接口方法签名相同
    public ResultVo fallback(Integer num){
        return new ResultVo(-1,"error","出问题啦，进入降级方法");
    }
}
